<?php
    /**
     * Created by PhpStorm.
     * User: long
     * Date: 2018/10/27
     * Time: 下午4:52
     */

    namespace app\index\controller;


    use app\share\logic\ShareBuyReward;
    use think\Controller;

    class Buyreward extends Controller
    {

        public function index()
        {

            $data = $this->request->param();
            if (!empty($data))
            {
                if ($data['number'] < 100)
                {
                    $this->error('购买数量小于100的没有奖励');
                }
                $number    = $data['number'];
                $member_id = $data['user_id'];
                $Reward    = new ShareBuyReward();

                $datas = $Reward->run($member_id,$number,$data)->saves();
                //            dump($data);

                echo $this->table('认购奖励区间比例',$Reward->getLevelConf(),['等级','区间','比例']);
                echo $this->table('用户',$Reward->member_info('',$data),['用户ID','推荐人','等级']);
                echo $this->table('用户' . $member_id . '购买数量' . $number . '---计算结果',$datas,['用户ID','推荐人','等级','奖励']);
                echo '</br>';
            }
            return $this->fetch('index/buy');


        }

        public function data_com()
        {

        }

        public function table($name,$data,$head)
        {


            $table = '<table border="1" cellspacing="0" cellpadding="2" align="center" width="15%" style="float: left;margin-left: 10px">';
            //标头
            $table .= "<caption><h3>{$name}</h3></caption>";
            $cols  = count($data);
            //标题
            $table .= '<tr align="center">';
            foreach ($head as $item)
            {

                $table .= '<th align="center">' . $item . '</th>';
            }
            $table .= "</tr>";
            //表格

            foreach ($data as $datum)
            {
                $table .= "<tr>";
                foreach ($datum as $item)
                {
                    $table .= "<td>{$item}</td>";
                }
                $table .= "</tr>";
            }


            $table .= "</table></br>";
            return $table;


        }

    }